package adminValid

import (
	"github.com/astaxie/beego/validation"
)

type UploadValidation struct{
	BaseValidation
	Title string `json:"title" valid:"Required"`
	ShootTime string `json:"shootTime" valid:"Required"`
	Address string `json:"address" valid:"Required"`
	IsShow int `json:"isShow"`
	Longitude float32 `json:"longitude" valid:"Required"`
	Latitude float32 `json:"latitude" valid:"Required"`
	Type int `json:"type"`
	Url string `json:"url" valid:"Required"`
}

func (v *UploadValidation) GetFiledName(key string) string{
	filed := make(map[string]string)
	filed["Title"] = "标题"
	filed["ShootTime"] = "拍摄时间"
	filed["Address"] = "地址"
	filed["IsShow"] = "公开信息"
	filed["Longitude"] = "经度"
	filed["Latitude"] = "纬度"
	filed["Type"] = "类型"
	filed["Url"] = "链接"
	return filed[key]
}


// 如果你的 struct 实现了接口 validation.ValidFormer
// 当 StructTag 中的测试都成功时，将会执行 Valid 函数进行自定义验证
func (u *UploadValidation) Valid(v *validation.Validation) {

}

func (v *UploadValidation) ValidatorAuto(data interface{}) (result bool, msg string){
	validation.SetDefaultMessage(v.getDefaultMessage())
	validate := validation.Validation{}
	isValid, err := validate.Valid(data)

	r := true
	m := ""
	if err != nil {
		r = false
		m = err.Error()
	}

	if !isValid {
		r = false
		m = v.GetFiledName(validate.Errors[0].Field) + validate.Errors[0].Message
	}
	return r, m
}
